--DO NOT MODIFY THIS FILE. IT IS ALWAYS OVERWRITTEN ON GENERATION.
--Data Schema For Version 0.0.0.0

--CREATE TABLE [Log]
if not exists(select * from sysobjects where name = 'Log' and xtype = 'U')
CREATE TABLE [dbo].[Log] (
[ID] [Int] IDENTITY (1, 1) NOT NULL ,
[TaskId] [Int] NULL ,
[StatusId] [Int] NULL ,
[Description] [VarChar] (6000) NULL ,
[Note] [Xml] NULL ,
[Hours] [Decimal] (4, 2) NULL ,
[UpdateTime] [DateTime] NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__LOG_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__LOG_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [ObjectAttribute]
if not exists(select * from sysobjects where name = 'ObjectAttribute' and xtype = 'U')
CREATE TABLE [dbo].[ObjectAttribute] (
[ID] [Int] IDENTITY (1, 1) NOT NULL ,
[ObjectName] [VarChar] (50) NULL ,
[AttributeName] [VarChar] (50) NULL ,
[AttributeType] [VarChar] (50) NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__OBJECTATTRIBUTE_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__OBJECTATTRIBUTE_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [ObjectData]
if not exists(select * from sysobjects where name = 'ObjectData' and xtype = 'U')
CREATE TABLE [dbo].[ObjectData] (
[ID] [Int] IDENTITY (1, 1) NOT NULL ,
[AttributeId] [Int] NULL ,
[Value] [VarChar] (255) NULL ,
[Key] [Int] NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__OBJECTDATA_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__OBJECTDATA_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [StatusDomain]
if not exists(select * from sysobjects where name = 'StatusDomain' and xtype = 'U')
CREATE TABLE [dbo].[StatusDomain] (
[ID] [Int] IDENTITY (1, 1) NOT NULL ,
[Status] [VarChar] (50) NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__STATUSDOMAIN_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__STATUSDOMAIN_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [Task]
if not exists(select * from sysobjects where name = 'Task' and xtype = 'U')
CREATE TABLE [dbo].[Task] (
[ID] [Int] IDENTITY (1, 1) NOT NULL ,
[StatusId] [Int] NOT NULL ,
[Description] [VarChar] (255) NULL ,
[Note] [Xml] NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__TASK_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__TASK_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--##SECTION BEGIN [FIELD CREATE]
if exists(select * from sys.objects where name = 'Log' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ID' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [ID] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'Log' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TaskId' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [TaskId] [Int] NULL 
if exists(select * from sys.objects where name = 'Log' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'StatusId' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [StatusId] [Int] NULL 
if exists(select * from sys.objects where name = 'Log' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Description' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [Description] [VarChar] (6000) NULL 
if exists(select * from sys.objects where name = 'Log' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Note' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [Note] [Xml] NULL 
if exists(select * from sys.objects where name = 'Log' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Hours' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [Hours] [Decimal] (4, 2) NULL 
if exists(select * from sys.objects where name = 'Log' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'UpdateTime' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [UpdateTime] [DateTime] NULL 
if exists(select * from sys.objects where name = 'ObjectAttribute' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ID' and o.name = 'ObjectAttribute')
ALTER TABLE [dbo].[ObjectAttribute] ADD [ID] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'ObjectAttribute' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ObjectName' and o.name = 'ObjectAttribute')
ALTER TABLE [dbo].[ObjectAttribute] ADD [ObjectName] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'ObjectAttribute' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'AttributeName' and o.name = 'ObjectAttribute')
ALTER TABLE [dbo].[ObjectAttribute] ADD [AttributeName] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'ObjectAttribute' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'AttributeType' and o.name = 'ObjectAttribute')
ALTER TABLE [dbo].[ObjectAttribute] ADD [AttributeType] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'ObjectData' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ID' and o.name = 'ObjectData')
ALTER TABLE [dbo].[ObjectData] ADD [ID] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'ObjectData' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'AttributeId' and o.name = 'ObjectData')
ALTER TABLE [dbo].[ObjectData] ADD [AttributeId] [Int] NULL 
if exists(select * from sys.objects where name = 'ObjectData' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Value' and o.name = 'ObjectData')
ALTER TABLE [dbo].[ObjectData] ADD [Value] [VarChar] (255) NULL 
if exists(select * from sys.objects where name = 'ObjectData' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Key' and o.name = 'ObjectData')
ALTER TABLE [dbo].[ObjectData] ADD [Key] [Int] NULL 
if exists(select * from sys.objects where name = 'StatusDomain' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ID' and o.name = 'StatusDomain')
ALTER TABLE [dbo].[StatusDomain] ADD [ID] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'StatusDomain' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Status' and o.name = 'StatusDomain')
ALTER TABLE [dbo].[StatusDomain] ADD [Status] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'Task' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ID' and o.name = 'Task')
ALTER TABLE [dbo].[Task] ADD [ID] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'Task' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'StatusId' and o.name = 'Task')
ALTER TABLE [dbo].[Task] ADD [StatusId] [Int] NOT NULL 
if exists(select * from sys.objects where name = 'Task' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Description' and o.name = 'Task')
ALTER TABLE [dbo].[Task] ADD [Description] [VarChar] (255) NULL 
if exists(select * from sys.objects where name = 'Task' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Note' and o.name = 'Task')
ALTER TABLE [dbo].[Task] ADD [Note] [Xml] NULL 
GO
--##SECTION END [FIELD CREATE]

--##SECTION BEGIN [AUDIT TRAIL CREATE]

--APPEND AUDIT TRAIL CREATE FOR TABLE [Log]
if exists(select * from sys.objects where name = 'Log' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Log' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__LOG_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Log]
if exists(select * from sys.objects where name = 'Log' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Log' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__LOG_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Log]
if exists(select * from sys.objects where name = 'Log' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Log')
ALTER TABLE [dbo].[Log] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [ObjectAttribute]
if exists(select * from sys.objects where name = 'ObjectAttribute' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'ObjectAttribute')
ALTER TABLE [dbo].[ObjectAttribute] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'ObjectAttribute' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'ObjectAttribute')
ALTER TABLE [dbo].[ObjectAttribute] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__OBJECTATTRIBUTE_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [ObjectAttribute]
if exists(select * from sys.objects where name = 'ObjectAttribute' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'ObjectAttribute')
ALTER TABLE [dbo].[ObjectAttribute] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'ObjectAttribute' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'ObjectAttribute')
ALTER TABLE [dbo].[ObjectAttribute] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__OBJECTATTRIBUTE_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [ObjectAttribute]
if exists(select * from sys.objects where name = 'ObjectAttribute' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'ObjectAttribute')
ALTER TABLE [dbo].[ObjectAttribute] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [ObjectData]
if exists(select * from sys.objects where name = 'ObjectData' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'ObjectData')
ALTER TABLE [dbo].[ObjectData] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'ObjectData' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'ObjectData')
ALTER TABLE [dbo].[ObjectData] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__OBJECTDATA_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [ObjectData]
if exists(select * from sys.objects where name = 'ObjectData' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'ObjectData')
ALTER TABLE [dbo].[ObjectData] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'ObjectData' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'ObjectData')
ALTER TABLE [dbo].[ObjectData] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__OBJECTDATA_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [ObjectData]
if exists(select * from sys.objects where name = 'ObjectData' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'ObjectData')
ALTER TABLE [dbo].[ObjectData] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [StatusDomain]
if exists(select * from sys.objects where name = 'StatusDomain' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'StatusDomain')
ALTER TABLE [dbo].[StatusDomain] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'StatusDomain' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'StatusDomain')
ALTER TABLE [dbo].[StatusDomain] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__STATUSDOMAIN_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [StatusDomain]
if exists(select * from sys.objects where name = 'StatusDomain' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'StatusDomain')
ALTER TABLE [dbo].[StatusDomain] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'StatusDomain' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'StatusDomain')
ALTER TABLE [dbo].[StatusDomain] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__STATUSDOMAIN_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [StatusDomain]
if exists(select * from sys.objects where name = 'StatusDomain' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'StatusDomain')
ALTER TABLE [dbo].[StatusDomain] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Task]
if exists(select * from sys.objects where name = 'Task' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Task')
ALTER TABLE [dbo].[Task] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Task' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Task')
ALTER TABLE [dbo].[Task] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__TASK_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Task]
if exists(select * from sys.objects where name = 'Task' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Task')
ALTER TABLE [dbo].[Task] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Task' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Task')
ALTER TABLE [dbo].[Task] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__TASK_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Task]
if exists(select * from sys.objects where name = 'Task' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Task')
ALTER TABLE [dbo].[Task] ADD [Timestamp] [timestamp] NOT NULL

GO

--##SECTION END [AUDIT TRAIL CREATE]

--##SECTION BEGIN [AUDIT TRAIL REMOVE]

--##SECTION END [AUDIT TRAIL REMOVE]

--##SECTION BEGIN [RENAME PK]

--RENAME EXISTING PRIMARY KEYS IF NECESSARY
DECLARE @pkfixLog varchar(500)
SET @pkfixLog = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Log')
if @pkfixLog <> '' and (BINARY_CHECKSUM(@pkfixLog) <> BINARY_CHECKSUM('PK_LOG')) exec('sp_rename '''+@pkfixLog+''', ''PK_LOG''')
DECLARE @pkfixObjectAttribute varchar(500)
SET @pkfixObjectAttribute = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'ObjectAttribute')
if @pkfixObjectAttribute <> '' and (BINARY_CHECKSUM(@pkfixObjectAttribute) <> BINARY_CHECKSUM('PK_OBJECTATTRIBUTE')) exec('sp_rename '''+@pkfixObjectAttribute+''', ''PK_OBJECTATTRIBUTE''')
DECLARE @pkfixObjectData varchar(500)
SET @pkfixObjectData = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'ObjectData')
if @pkfixObjectData <> '' and (BINARY_CHECKSUM(@pkfixObjectData) <> BINARY_CHECKSUM('PK_OBJECTDATA')) exec('sp_rename '''+@pkfixObjectData+''', ''PK_OBJECTDATA''')
DECLARE @pkfixStatusDomain varchar(500)
SET @pkfixStatusDomain = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'StatusDomain')
if @pkfixStatusDomain <> '' and (BINARY_CHECKSUM(@pkfixStatusDomain) <> BINARY_CHECKSUM('PK_STATUSDOMAIN')) exec('sp_rename '''+@pkfixStatusDomain+''', ''PK_STATUSDOMAIN''')
DECLARE @pkfixTask varchar(500)
SET @pkfixTask = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Task')
if @pkfixTask <> '' and (BINARY_CHECKSUM(@pkfixTask) <> BINARY_CHECKSUM('PK_TASK')) exec('sp_rename '''+@pkfixTask+''', ''PK_TASK''')
GO

--##SECTION END [RENAME PK]

--##SECTION BEGIN [DROP PK]

--##SECTION END [DROP PK]

--##SECTION BEGIN [CREATE PK]

--PRIMARY KEY FOR TABLE [Log]
if not exists(select * from sysobjects where name = 'PK_LOG' and xtype = 'PK')
ALTER TABLE [dbo].[Log] WITH NOCHECK ADD 
CONSTRAINT [PK_LOG] PRIMARY KEY CLUSTERED
(
	[ID]
)
GO

--PRIMARY KEY FOR TABLE [ObjectAttribute]
if not exists(select * from sysobjects where name = 'PK_OBJECTATTRIBUTE' and xtype = 'PK')
ALTER TABLE [dbo].[ObjectAttribute] WITH NOCHECK ADD 
CONSTRAINT [PK_OBJECTATTRIBUTE] PRIMARY KEY CLUSTERED
(
	[ID]
)
GO

--PRIMARY KEY FOR TABLE [ObjectData]
if not exists(select * from sysobjects where name = 'PK_OBJECTDATA' and xtype = 'PK')
ALTER TABLE [dbo].[ObjectData] WITH NOCHECK ADD 
CONSTRAINT [PK_OBJECTDATA] PRIMARY KEY CLUSTERED
(
	[ID]
)
GO

--PRIMARY KEY FOR TABLE [StatusDomain]
if not exists(select * from sysobjects where name = 'PK_STATUSDOMAIN' and xtype = 'PK')
ALTER TABLE [dbo].[StatusDomain] WITH NOCHECK ADD 
CONSTRAINT [PK_STATUSDOMAIN] PRIMARY KEY CLUSTERED
(
	[ID]
)
GO

--PRIMARY KEY FOR TABLE [Task]
if not exists(select * from sysobjects where name = 'PK_TASK' and xtype = 'PK')
ALTER TABLE [dbo].[Task] WITH NOCHECK ADD 
CONSTRAINT [PK_TASK] PRIMARY KEY CLUSTERED
(
	[ID]
)
GO

--##SECTION END [CREATE PK]

--##SECTION BEGIN [AUDIT TABLES PK]

--DROP PRIMARY KEY FOR TABLE [__AUDIT__Log]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__LOG'))
ALTER TABLE [dbo].[__AUDIT__Log] DROP CONSTRAINT [PK___AUDIT__LOG]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__ObjectAttribute]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__OBJECTATTRIBUTE'))
ALTER TABLE [dbo].[__AUDIT__ObjectAttribute] DROP CONSTRAINT [PK___AUDIT__OBJECTATTRIBUTE]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__ObjectData]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__OBJECTDATA'))
ALTER TABLE [dbo].[__AUDIT__ObjectData] DROP CONSTRAINT [PK___AUDIT__OBJECTDATA]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__StatusDomain]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__STATUSDOMAIN'))
ALTER TABLE [dbo].[__AUDIT__StatusDomain] DROP CONSTRAINT [PK___AUDIT__STATUSDOMAIN]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__Task]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__TASK'))
ALTER TABLE [dbo].[__AUDIT__Task] DROP CONSTRAINT [PK___AUDIT__TASK]
GO

--##SECTION END [AUDIT TABLES PK]

--FOREIGN KEY RELATIONSHIP [StatusDomain] -> [Log] ([StatusDomain].[ID] -> [Log].[StatusId])
if not exists(select * from sysobjects where name = 'FK_STATUS_LOG_LOG_STATUSDOMAIN' and xtype = 'F')
ALTER TABLE [dbo].[Log] ADD 
CONSTRAINT [FK_STATUS_LOG_LOG_STATUSDOMAIN] FOREIGN KEY 
(
	[StatusId]
) REFERENCES [dbo].[StatusDomain] (
	[ID]
)
GO

--FOREIGN KEY RELATIONSHIP [Task] -> [Log] ([Task].[ID] -> [Log].[TaskId])
if not exists(select * from sysobjects where name = 'FK_TASK_LOG_LOG_TASK' and xtype = 'F')
ALTER TABLE [dbo].[Log] ADD 
CONSTRAINT [FK_TASK_LOG_LOG_TASK] FOREIGN KEY 
(
	[TaskId]
) REFERENCES [dbo].[Task] (
	[ID]
)
GO

--FOREIGN KEY RELATIONSHIP [ObjectAttribute] -> [ObjectData] ([ObjectAttribute].[ID] -> [ObjectData].[AttributeId])
if not exists(select * from sysobjects where name = 'FK__OBJECTDATA_OBJECTATTRIBUTE' and xtype = 'F')
ALTER TABLE [dbo].[ObjectData] ADD 
CONSTRAINT [FK__OBJECTDATA_OBJECTATTRIBUTE] FOREIGN KEY 
(
	[AttributeId]
) REFERENCES [dbo].[ObjectAttribute] (
	[ID]
)
GO

--FOREIGN KEY RELATIONSHIP [StatusDomain] -> [Task] ([StatusDomain].[ID] -> [Task].[StatusId])
if not exists(select * from sysobjects where name = 'FK_STATUS_TASK_TASK_STATUSDOMAIN' and xtype = 'F')
ALTER TABLE [dbo].[Task] ADD 
CONSTRAINT [FK_STATUS_TASK_TASK_STATUSDOMAIN] FOREIGN KEY 
(
	[StatusId]
) REFERENCES [dbo].[StatusDomain] (
	[ID]
)
GO

--##SECTION BEGIN [CREATE INDEXES]

--##SECTION END [CREATE INDEXES]

--##SECTION BEGIN [TENANT INDEXES]

--##SECTION END [TENANT INDEXES]

--##SECTION BEGIN [REMOVE DEFAULTS]

--BEGIN DEFAULTS FOR TABLE [Log]
--DROP CONSTRAINT FOR '[Log].[Description]' if one exists
declare @Log_Description varchar(500)
set @Log_Description = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Log' and a.name = 'Description')
if (@Log_Description IS NOT NULL) exec ('ALTER TABLE [Log] DROP CONSTRAINT [' + @Log_Description + ']')

--DROP CONSTRAINT FOR '[Log].[Hours]' if one exists
declare @Log_Hours varchar(500)
set @Log_Hours = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Log' and a.name = 'Hours')
if (@Log_Hours IS NOT NULL) exec ('ALTER TABLE [Log] DROP CONSTRAINT [' + @Log_Hours + ']')

--DROP CONSTRAINT FOR '[Log].[ID]' if one exists
declare @Log_ID varchar(500)
set @Log_ID = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Log' and a.name = 'ID')
if (@Log_ID IS NOT NULL) exec ('ALTER TABLE [Log] DROP CONSTRAINT [' + @Log_ID + ']')

--DROP CONSTRAINT FOR '[Log].[Note]' if one exists
declare @Log_Note varchar(500)
set @Log_Note = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Log' and a.name = 'Note')
if (@Log_Note IS NOT NULL) exec ('ALTER TABLE [Log] DROP CONSTRAINT [' + @Log_Note + ']')

--DROP CONSTRAINT FOR '[Log].[StatusId]' if one exists
declare @Log_StatusId varchar(500)
set @Log_StatusId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Log' and a.name = 'StatusId')
if (@Log_StatusId IS NOT NULL) exec ('ALTER TABLE [Log] DROP CONSTRAINT [' + @Log_StatusId + ']')

--DROP CONSTRAINT FOR '[Log].[TaskId]' if one exists
declare @Log_TaskId varchar(500)
set @Log_TaskId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Log' and a.name = 'TaskId')
if (@Log_TaskId IS NOT NULL) exec ('ALTER TABLE [Log] DROP CONSTRAINT [' + @Log_TaskId + ']')

--DROP CONSTRAINT FOR '[Log].[UpdateTime]' if one exists
declare @Log_UpdateTime varchar(500)
set @Log_UpdateTime = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Log' and a.name = 'UpdateTime')
if (@Log_UpdateTime IS NOT NULL) exec ('ALTER TABLE [Log] DROP CONSTRAINT [' + @Log_UpdateTime + ']')

--END DEFAULTS FOR TABLE [Log]
GO

--BEGIN DEFAULTS FOR TABLE [ObjectAttribute]
--DROP CONSTRAINT FOR '[ObjectAttribute].[AttributeName]' if one exists
declare @ObjectAttribute_AttributeName varchar(500)
set @ObjectAttribute_AttributeName = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='ObjectAttribute' and a.name = 'AttributeName')
if (@ObjectAttribute_AttributeName IS NOT NULL) exec ('ALTER TABLE [ObjectAttribute] DROP CONSTRAINT [' + @ObjectAttribute_AttributeName + ']')

--DROP CONSTRAINT FOR '[ObjectAttribute].[AttributeType]' if one exists
declare @ObjectAttribute_AttributeType varchar(500)
set @ObjectAttribute_AttributeType = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='ObjectAttribute' and a.name = 'AttributeType')
if (@ObjectAttribute_AttributeType IS NOT NULL) exec ('ALTER TABLE [ObjectAttribute] DROP CONSTRAINT [' + @ObjectAttribute_AttributeType + ']')

--DROP CONSTRAINT FOR '[ObjectAttribute].[ID]' if one exists
declare @ObjectAttribute_ID varchar(500)
set @ObjectAttribute_ID = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='ObjectAttribute' and a.name = 'ID')
if (@ObjectAttribute_ID IS NOT NULL) exec ('ALTER TABLE [ObjectAttribute] DROP CONSTRAINT [' + @ObjectAttribute_ID + ']')

--DROP CONSTRAINT FOR '[ObjectAttribute].[ObjectName]' if one exists
declare @ObjectAttribute_ObjectName varchar(500)
set @ObjectAttribute_ObjectName = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='ObjectAttribute' and a.name = 'ObjectName')
if (@ObjectAttribute_ObjectName IS NOT NULL) exec ('ALTER TABLE [ObjectAttribute] DROP CONSTRAINT [' + @ObjectAttribute_ObjectName + ']')

--END DEFAULTS FOR TABLE [ObjectAttribute]
GO

--BEGIN DEFAULTS FOR TABLE [ObjectData]
--DROP CONSTRAINT FOR '[ObjectData].[AttributeId]' if one exists
declare @ObjectData_AttributeId varchar(500)
set @ObjectData_AttributeId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='ObjectData' and a.name = 'AttributeId')
if (@ObjectData_AttributeId IS NOT NULL) exec ('ALTER TABLE [ObjectData] DROP CONSTRAINT [' + @ObjectData_AttributeId + ']')

--DROP CONSTRAINT FOR '[ObjectData].[ID]' if one exists
declare @ObjectData_ID varchar(500)
set @ObjectData_ID = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='ObjectData' and a.name = 'ID')
if (@ObjectData_ID IS NOT NULL) exec ('ALTER TABLE [ObjectData] DROP CONSTRAINT [' + @ObjectData_ID + ']')

--DROP CONSTRAINT FOR '[ObjectData].[Key]' if one exists
declare @ObjectData_Key varchar(500)
set @ObjectData_Key = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='ObjectData' and a.name = 'Key')
if (@ObjectData_Key IS NOT NULL) exec ('ALTER TABLE [ObjectData] DROP CONSTRAINT [' + @ObjectData_Key + ']')

--DROP CONSTRAINT FOR '[ObjectData].[Value]' if one exists
declare @ObjectData_Value varchar(500)
set @ObjectData_Value = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='ObjectData' and a.name = 'Value')
if (@ObjectData_Value IS NOT NULL) exec ('ALTER TABLE [ObjectData] DROP CONSTRAINT [' + @ObjectData_Value + ']')

--END DEFAULTS FOR TABLE [ObjectData]
GO

--BEGIN DEFAULTS FOR TABLE [StatusDomain]
--DROP CONSTRAINT FOR '[StatusDomain].[ID]' if one exists
declare @StatusDomain_ID varchar(500)
set @StatusDomain_ID = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='StatusDomain' and a.name = 'ID')
if (@StatusDomain_ID IS NOT NULL) exec ('ALTER TABLE [StatusDomain] DROP CONSTRAINT [' + @StatusDomain_ID + ']')

--DROP CONSTRAINT FOR '[StatusDomain].[Status]' if one exists
declare @StatusDomain_Status varchar(500)
set @StatusDomain_Status = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='StatusDomain' and a.name = 'Status')
if (@StatusDomain_Status IS NOT NULL) exec ('ALTER TABLE [StatusDomain] DROP CONSTRAINT [' + @StatusDomain_Status + ']')

--END DEFAULTS FOR TABLE [StatusDomain]
GO

--BEGIN DEFAULTS FOR TABLE [Task]
--DROP CONSTRAINT FOR '[Task].[Description]' if one exists
declare @Task_Description varchar(500)
set @Task_Description = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Task' and a.name = 'Description')
if (@Task_Description IS NOT NULL) exec ('ALTER TABLE [Task] DROP CONSTRAINT [' + @Task_Description + ']')

--DROP CONSTRAINT FOR '[Task].[ID]' if one exists
declare @Task_ID varchar(500)
set @Task_ID = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Task' and a.name = 'ID')
if (@Task_ID IS NOT NULL) exec ('ALTER TABLE [Task] DROP CONSTRAINT [' + @Task_ID + ']')

--DROP CONSTRAINT FOR '[Task].[Note]' if one exists
declare @Task_Note varchar(500)
set @Task_Note = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Task' and a.name = 'Note')
if (@Task_Note IS NOT NULL) exec ('ALTER TABLE [Task] DROP CONSTRAINT [' + @Task_Note + ']')

--DROP CONSTRAINT FOR '[Task].[StatusId]' if one exists
declare @Task_StatusId varchar(500)
set @Task_StatusId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Task' and a.name = 'StatusId')
if (@Task_StatusId IS NOT NULL) exec ('ALTER TABLE [Task] DROP CONSTRAINT [' + @Task_StatusId + ']')

--END DEFAULTS FOR TABLE [Task]
GO

--##SECTION END [REMOVE DEFAULTS]

--##SECTION BEGIN [CREATE DEFAULTS]

--##SECTION END [CREATE DEFAULTS]

if not exists(select * from sys.objects where [name] = '__nhydrateschema' and [type] = 'U')
BEGIN
CREATE TABLE [__nhydrateschema] (
[dbVersion] [varchar] (50) NOT NULL,
[LastUpdate] [datetime] NOT NULL,
[ModelKey] [uniqueidentifier] NOT NULL,
[History] [nvarchar](max) NOT NULL
)
if not exists(select * from sys.objects where [name] = '__pk__nhydrateschema' and [type] = 'PK')
ALTER TABLE [__nhydrateschema] WITH NOCHECK ADD CONSTRAINT [__pk__nhydrateschema] PRIMARY KEY CLUSTERED ([ModelKey])
END
GO

if not exists(select * from sys.objects where name = '__nhydrateobjects' and [type] = 'U')
CREATE TABLE [dbo].[__nhydrateobjects]
(
	[rowid] [bigint] IDENTITY(1,1) NOT NULL,
	[id] [uniqueidentifier] NULL,
	[name] [varchar](500) NOT NULL,
	[type] [varchar](10) NOT NULL,
	[schema] [varchar](500) NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[Hash] [varchar](32) NULL,
	[ModelKey] [uniqueidentifier] NOT NULL,
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_name')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_name] ON [dbo].[__nhydrateobjects]
(
	[name] ASC
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_schema')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_schema] ON [dbo].[__nhydrateobjects] 
(
	[schema] ASC
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_type')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_type] ON [dbo].[__nhydrateobjects] 
(
	[type] ASC
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_modelkey')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_modelkey] ON [dbo].[__nhydrateobjects] 
(
	[ModelKey] ASC
)

if not exists(select * from sys.indexes where name = '__pk__nhydrateobjects')
ALTER TABLE [dbo].[__nhydrateobjects] ADD CONSTRAINT [__pk__nhydrateobjects] PRIMARY KEY CLUSTERED 
(
	[rowid] ASC
)

